<?php

namespace builder\card\util\card_item;

use builder\base\ViewBuilderInterface;
use Yii;
use yii\helpers\Html;

/**
 * 卡片面板
 * @auther clevstn
 * @since ym2.0
 */
class Card extends CardItem
{
    private $_options = [
        'type' => 'card',
        'header' => '',
        'body' => '',
        'footer' => '',
        'comment' => '',
        'help' => '',
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',
    ];

    /**
     * 头部
     * @param string|callable|ViewBuilderInterface $header 闭包支持依赖注入。
     * @return $this
     * @throws \yii\base\InvalidConfigException
     */
    public function header($header)
    {
        $this->_options['header'] = $this->formatContent($header);;
        return $this;
    }

    /**
     * 内容
     * @param string|callable|ViewBuilderInterface $body 闭包支持依赖注入。
     * @return $this
     * @throws \yii\base\InvalidConfigException
     */
    public function body($body)
    {
        $this->_options['body'] = $this->formatContent($body);;
        return $this;
    }

    /**
     * 尾部
     * @param string|callable|ViewBuilderInterface $footer 闭包支持依赖注入。
     * @return $this
     * @throws \yii\base\InvalidConfigException
     */
    public function footer($footer)
    {
        $this->_options['footer'] = $this->formatContent($footer);
        return $this;
    }

    /**
     * 格式化传参内容
     * @param callable|string|ViewBuilderInterface $content 支持依赖注入
     * @return string
     * @throws \yii\base\InvalidConfigException
     */
    protected function formatContent($content)
    {
        if (is_callable($content)) {//callable
            $content = Yii::createObject($content);
        } elseif (is_string($content)) {//string
            $content = Html::encode($content);//防止xss，如果须Html则可用callback。
        }

        if ($content instanceof ViewBuilderInterface) {//ViewBuilderInterface Instance
            $content = $content->render(true);
        }

        return $content;
    }

    /**
     * 注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->_options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    public function _toArray()
    {
        return $this->_options;
    }
}